package cn.promonkey.api.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.promonkey.core.dto.UserDto;
import cn.promonkey.core.response.ResultMsg;
import cn.promonkey.core.toolkit.QueryGenerator;
import cn.promonkey.core.toolkit.UserInfoUtils;
import cn.promonkey.api.common.entity.CommonTreeDto;
import cn.promonkey.api.group.entity.Group;
import cn.promonkey.api.group.service.GroupService;
import cn.promonkey.api.permission.entity.Permission;
import cn.promonkey.api.permission.service.PermissionService;
import cn.promonkey.api.relation.entity.Relation;
import cn.promonkey.api.relation.service.RelationService;
import cn.promonkey.api.system.entity.System;
import cn.promonkey.api.system.entity.SystemDto;
import cn.promonkey.api.system.service.SystemService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/** @author <no value> */
@Slf4j
@RestController
@RequestMapping("/group/")
public class GroupController {
  final GroupService groupService;
  final PermissionService permissionService;
  final RelationService relationService;
  final SystemService systemService;

  public GroupController(GroupService groupService, PermissionService permissionService,RelationService relationService,SystemService systemService) {
    this.groupService = groupService;
    this.permissionService = permissionService;
    this.relationService = relationService;
    this.systemService = systemService;
  }

  @RequestMapping("getPage")
  public String getPage(
      Group group,
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<Group> queryWrapper = QueryGenerator.initQueryWrapper(group, request.getParameterMap());
    Page<Group> pageable = new Page<>(page, size);
    IPage<Group> pageList = groupService.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody Group group) {
	  
	  UserDto sysuser = UserInfoUtils.getUserInfo();
	  group.setBusinessId(sysuser.getBusinessId());
	  group.setStatus(1);
	  group.setType(1);
	  groupService.save(group);

	  List<Permission> list = new ArrayList<>();
	  for(Long menuId:group.getMenuIds()) {
		  Permission permission = new Permission();
		  permission.setBusinessId(group.getBusinessId());
		  permission.setButtonId(0);
		  permission.setGroupId(group.getGroupId());
		  permission.setInterfaceId(0);
		  permission.setMenuId(menuId);
		  permission.setSystemId(group.getSystemId());
		  list.add(permission);
	  }
	  if(list.size()>0)
		  permissionService.saveBatch(list);
	  
    return ResultMsg.success();
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody Group group) {
	  
	  QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
	  queryWrapper.eq("group_id", group.getGroupId());
	  permissionService.remove(queryWrapper);
	  
	  List<Permission> list = new ArrayList<>();
	  for(Long menuId:group.getMenuIds()) {
		  Permission permission = new Permission();
		  permission.setBusinessId(group.getBusinessId());
		  permission.setButtonId(0);
		  permission.setGroupId(group.getGroupId());
		  permission.setInterfaceId(0);
		  permission.setMenuId(menuId);
		  permission.setSystemId(group.getSystemId());
		  list.add(permission);
	  }
	  if(list.size()>0)
		  permissionService.saveBatch(list);
	  
    return ResultMsg.success(groupService.updateById(group));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
	  
	  Group group = groupService.getById(id);
	  
	  QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
	  queryWrapper.eq("group_id", id);
	  List<Permission> list = permissionService.list(queryWrapper);
	  
	  group.setMenuIds(list.stream().map(Permission::getMenuId).collect(Collectors.toList()));
	  
    return ResultMsg.success(group);
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(groupService.removeById(id));
  }
  
  @RequestMapping(value = "getGroup", method = RequestMethod.POST)
  public String getGroup(@RequestBody Long id) {
	  
	  UserDto user = UserInfoUtils.getUserInfo();
	  CommonTreeDto dto = new CommonTreeDto();
	  QueryWrapper<System> systemWrapper = new  QueryWrapper<>();
	  systemWrapper.eq("business_id", user.getBusinessId());
	  List<System> systemList = systemService.list(systemWrapper);
	  List<SystemDto> systemDtoList = new ArrayList<>();
	  for(System system:systemList) {
		  SystemDto systemDto = new SystemDto(); 
		  systemDto.setSystemId(system.getSystemId());
		  systemDto.setSystemName(system.getSystemName());
		  QueryWrapper<Group> groupWrapper = new  QueryWrapper<>();
		  groupWrapper.eq("business_id", user.getBusinessId()).eq("system_id", system.getSystemId());
		  List<Group> groupList = groupService.list(groupWrapper);
		  systemDto.setGroup(groupList);
		  systemDtoList.add(systemDto);
	  }
	  dto.setSystem(systemDtoList);
	  QueryWrapper<Relation> queryWrapper = new  QueryWrapper<>();
	  queryWrapper.eq("user_id", id);
	  List<Relation> relationList = relationService.list(queryWrapper);
	  dto.setCheck(relationList.stream().map(Relation::getGroupId).collect(Collectors.toList()));
	  
    return ResultMsg.success(dto);
  }
  
}
